package com.xr.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.google.common.base.FinalizableReferenceQueue;
import com.xr.constant.MessageConstant;
import com.xr.pojo.Menu;
import com.xr.result.Result;
import com.xr.service.MemuService;
import com.xr.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @Reference
    private MemuService memuService;

    //获取登录的用户名
    @RequestMapping("/getUserName")
    public Result getUserName() {
        try {
            //获取登录用户的认证信息
            User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            System.out.println("loginUser:" + loginUser);
            //登录用户名
            String username = loginUser.getUsername();
            //返回给前端
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }

    //根据登录的不同用户显示不同菜单
    @RequestMapping("/showMenuByUser")
    public Result showMenuByUser() {
        System.out.println("join showMenuByUser=====>");
        //获取登录用户的角色
        User loginUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = loginUser.getUsername();
        List<Menu> menus = memuService.queryMenuByUser(username);
        System.out.println("mensValue:" + menus);
        //存放数据集合
        List<Menu> menuList = new ArrayList<>();
        try {
            if (menus != null && menus.size() > 0) {
                System.out.println("join 1");
                for (Menu parent : menus) {
                    if (parent != null && parent.getLevel() == 1) {
                        List<Menu> childList = new ArrayList<>();
                        for (Menu child : menus) {
                            if (child != null && child.getLevel() == 2 && parent.getId() == child.getParentmenuid()) {
                                if (child.getParentmenuid() == parent.getId()) {
                                    childList.add(child);
                                }
                            }
                        }
                        parent.setChildren(childList);
                        menuList.add(parent);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
        System.out.println("menusValue" + menuList);
        return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuList);
    }

}
